package org.sourcecode.common.httpclient4;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

public class HttpAccessor {

	public static final String post(String url,Map<String, String> paras,String requestCharset,String responseCharset) throws Exception{
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		Set<String> keys = paras.keySet();
		for (String key : keys ) {
			formparams.add(new BasicNameValuePair(key, paras.get(key)));
		}
		HttpPost post = new HttpPost(url);
		post.setEntity(new UrlEncodedFormEntity(formparams, requestCharset));
		String response = HttpClientExecutor.getExecutor().postStringResult(post, responseCharset);
		return response;
	}

	public static final String utf8Post(String url,Map<String, String> paras) throws Exception{
		return post(url, paras, "UTF-8", "UTF-8");
	}

	public static final String gbkPost(String url,Map<String, String> paras) throws Exception{
		return post(url, paras, "GBK", "GBK");
	}
	
	public static final String get(String url,String charset) throws Exception {
		HttpGet httpRequest = new HttpGet(url);
		return HttpClientExecutor.getExecutor().getStringResult(httpRequest,charset);
	}
	
	public static final String utf8Get(String url) throws Exception{
		return get(url, "UTF-8");
	}

	public static final String gbkGet(String url) throws Exception{
		return get(url,"GBK");
	}
}
